package Library.servlets;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Libarary.items.Books;
import Libarary.items.SearchedBook;

/**
 * Servlet implementation class HomeServlet
 */
@WebServlet("/HomeServlet")
public class HomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HomeServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();

		if (session != null) {
			pageDrawer(response);
		}else{
			response.sendRedirect("Invalid.html");
		}
		
	}

	private void pageDrawer(HttpServletResponse response) throws IOException {
		Books books = new Books();
		List<SearchedBook> listBooks =books.getListOfBooks();
		response.getWriter()
				.print("<!DOCTYPE html><html \"><head><title> Home </title><meta http-equiv=\"Content-Language\" content=\"English\" /><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />	<link rel=\"stylesheet\" type=\"text/css\" href=\"css/style.css\" media=\"screen\" /><link href='http://fonts.googleapis.com/css?family=Roboto+Condensed' rel='stylesheet' type='text/css' /></head><body><div id=\"wrap\">	<div id=\"header\"><div id=\"headerlinks\">	<a href=\"index.html\" title=\"Home\">Home</a><a href=\"Contact.html\" title=\"Contact\">Contact</a><a href=\"LogoutServlet\" title=\"LogOut\">Log out</a></div><h1><a href=\"Index.html\">Library</a></h1></div>	<div id=\"sidebar\"><h2>Your features:</h2><div class=\"box\"><ul><li><a href=\"Search.html\">Search for book</a></li><li><a href=\"#\">Which books I have taken</a></li></ul></div></div><div id=\"content\"><table border=\"1\" style=\"width:100%\"><caption>Sample books from our library</caption><tr><th>Title</th> <th>Author</th> <th>Status</th></tr>");
		System.out.println("--------------------------------------------------");
		for(SearchedBook book : listBooks) {
			response.getWriter().print("<tr><td>"+book.getTitle()+"</td> <td>"+book.getAuthorFirstName()+" "+book.getAuthorLastName()+"</td> <td>"+book.getBookStatus()+"</td></tr>");
			System.out.println(book+"Book Title"+book.getTitle());
		}
		System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++");
		response.getWriter().print("</table>Go to <a href=\"#\" title=\"#\">search</a></div><div id=\"footer\">	B2P Library	</div></div></body></html>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
